package com.lzb.party.controller;

import com.lzb.party.controller.dto.UserDto;
import com.lzb.party.entity.User;
import com.lzb.party.mapper.UserMapper;
import com.lzb.party.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired(required=false)//@Resource
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public boolean login(@RequestBody UserDto userDto){
        //@RequestBody注释把前台的Json数据变成Java对象
        String snum = userDto.getSnum();
        String pass = userDto.getPass();
        if (StringUtils.isBlank(snum)||StringUtils.isBlank(pass)){
            return false;
        }
        return userMapper.login(snum,pass).equals(1);
    }

    /*已废弃，使用save
    @PostMapping("/register")
    public Map<String,Object> register(@RequestBody UserDto user){
        //@RequestBody注释把前台的Json数据变成Java对象
        String sname = user.getSname();
        String pass = user.getPass();
        Map<String,Object> res = new HashMap<>();
        if (StringUtils.isBlank(sname)||StringUtils.isBlank(pass)){
            res.put("status",false);
            return res;
        }
        String id = userMapper.selectsnum(sname);
        if (!Objects.equals(id, "")){
            if (userService.saveUser(new User(sname,pass))){
                String snum = userMapper.selectsnum(sname);
                res.put("status",true);
                res.put("userid",snum);
                return res;
            }
            else {
                res.put("status",false);
                return res;
            }
        } else {
            res.put("status",false);
            return res;
        }
    }*/

    @PostMapping
    //新增或更新
    public boolean save(@RequestBody User user){
        //@RequestBody注释把前台的Json数据变成User对象
        return userService.saveUser(user);
    }

    @GetMapping()
    //查询所有数据
    public List<User> findAll(){
        return userMapper.findAll();
    }

    @DeleteMapping("/{snum}")
    public Integer delete(@PathVariable Integer snum) {
        return userMapper.deleteById(snum);
    }

    @GetMapping("/page")
    //分页查询数据
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String sname,
                                        @RequestParam String snum){
        pageNum = (pageNum-1)*pageSize;
        sname = "%"+sname+"%";
        snum = "%"+snum+"%";
        List<User> data = userMapper.selectPage(pageNum,pageSize,sname,snum);
        Integer total = userMapper.selectTotal(sname,snum);
        Map<String, Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
}
